package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.dto.MemberMoneyDto;
import cn.com.chengmandian.bike.mapper.EbMemberMoneyMapper;
import cn.com.chengmandian.bike.model.EbMemberMoney;
import cn.com.chengmandian.bike.service.IEbMemberMoneyService;
import cn.com.chengmandian.core.common.utils.BillNoUtil;
import cn.com.chengmandian.core.web.pojo.AgentUser;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.com.chengmandian.service.bike.constants.MemberMoneyBusinessTypeEnum;
import cn.com.chengmandian.service.bike.constants.MemberMoneyOperationTypeEnum;
import cn.com.chengmandian.service.bike.constants.MemberMoneyTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;

/**
 * <p>
 * 电单车运营-资金流水 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-12-11
 */
@Service
public class EbMemberMoneyServiceImpl extends ServiceImpl<EbMemberMoneyMapper, EbMemberMoney> implements IEbMemberMoneyService {
    @Autowired
    private EbMemberMoneyMapper memberMoneyMapper;

    @Override
    public void createGift(Integer areaId, Integer memberId, MemberMoneyBusinessTypeEnum businessType, Integer businessId, MemberMoneyOperationTypeEnum operationType, BigDecimal money) {
        this.create(areaId, memberId, businessType, businessId, operationType, money, MemberMoneyTypeEnum.GIFT);
    }

    @Override
    public void createCharge(Integer areaId, Integer memberId, MemberMoneyBusinessTypeEnum businessType, Integer businessId, MemberMoneyOperationTypeEnum operationType, BigDecimal money) {
        this.create(areaId, memberId, businessType, businessId, operationType, money, MemberMoneyTypeEnum.CHARGE);
    }

    @Override
    public void createPay(Integer areaId, Integer memberId, MemberMoneyBusinessTypeEnum businessType, Integer businessId, MemberMoneyOperationTypeEnum operationType, BigDecimal money) {
        this.create(areaId, memberId, businessType, businessId, operationType, money, MemberMoneyTypeEnum.PAY);
    }

    @Override
    public void create(Integer areaId, Integer memberId, MemberMoneyBusinessTypeEnum businessType, Integer businessId, MemberMoneyOperationTypeEnum operationType, BigDecimal money, MemberMoneyTypeEnum type) {
        if (money.compareTo(new BigDecimal(0)) <= 0) return;
        Assert.notNull(businessType, "业务类型不存在");
        EbMemberMoney memberMoney = new EbMemberMoney();
        memberMoney.setAreaId(areaId);
        memberMoney.setMoneyNo(BillNoUtil.getSimpleNo("MM-"));
        memberMoney.setBusinessType(businessType.getCode());
        memberMoney.setBusinessId(businessId);
        memberMoney.setOperationType(operationType.getCode());
        memberMoney.setMemberId(memberId);
        memberMoney.setInout(businessType.getInout());
        memberMoney.setType(type.getCode());
        memberMoney.setMoney(money);
        this.save(memberMoney);
    }

    @Override
    public IPage<EbMemberMoney> pages(MemberMoneyDto memberMoneyDto, AgentUser agentUser) {
        return memberMoneyMapper.selectMoneyList(Page.of(memberMoneyDto.getPageNumber(), memberMoneyDto.getPageSize()), memberMoneyDto, agentUser.getId());
    }
}
